package com.nondo.dean.platform.controller.api.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.domain.Grade;
import com.nondo.dean.platform.service.GradeService;

/**
 * 等级管理后台接口
 * @author zlj
 * @date 2018年3月27日
 */
@RestController("apiAdminGrade")
@RequestMapping("/{v}/admin/grade")
public class GradeController {
    
    @Autowired
    GradeService gradeService;
    
    /**
     * 查看等级详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<Grade> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<Grade> view = new View<Grade>(20000, "获取数据成功");
        
        Grade grade = null;
        if (id != null) {
            grade = gradeService.selectGrade(id);
        } else {
            grade = new Grade();
        }

        view.setData(grade);
        return view;
    }
    
    /**
     * 添加等级
     * @param request
     * @param response
     * @param grade 等级信息 {@link Grade}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<Grade> add(HttpServletRequest request, HttpServletResponse response,
            @Validated @RequestBody Grade grade, BindingResult result) {
        View<Grade> view = new View<Grade>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Grade gradeDb = this.gradeService.selectGradeByName(grade.getName());
            if (gradeDb != null) {
                return View.of500Error("等级名称已存在");
            }
            this.gradeService.addGrade(grade);
        }
        return view;
    }

    /**
     * 更新等级
     * @param request
     * @param response
     * @param grade 等级信息 {@link Grade}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<Grade> update(HttpServletRequest request,
            HttpServletResponse response,
            @Validated @RequestBody Grade grade, BindingResult result) {
        View<Grade> view = new View<Grade>(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Grade gradeDb = this.gradeService.selectGradeByName(grade.getName());
            if (gradeDb.getName().equals(grade.getName()) && gradeDb.getId() != grade.getId()) {
                return View.of500Error("等级名称已存在");
            }
            this.gradeService.updateGrade(gradeDb);
        }

        return view;
    }

    /**
     * 获取等级分页查询列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<Grade>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Grade> query) {
        View<Page<Grade>> view = new View<Page<Grade>>(20000, "获取列表成功");

        Grade grade = query.getData();

        Page<Grade> page = new Page<Grade>(query.getPage(), query.getSize(), gradeService.count(grade));
        
        List<Grade> gradeList = gradeService.getGradeList(query.getOffset(), query.getLimit(), grade);
        
        page.setContent(gradeList);
        
        view.setData(page);
        
        return view;
    }

    /**
     * 删除等级
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View view = new View(20000, "删除成功");

        Grade grade = gradeService.selectGrade(id);
        grade.setDelFlag(String.valueOf(1));
        this.gradeService.updateGrade(grade);

        return view;
    }
    
    /**
     * 获取等级列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list/all")
    public View<List<Grade>> list(HttpServletRequest request, HttpServletResponse response) {
        View<List<Grade>> view = new View<List<Grade>>(20000, "获取列表成功");

        List<Grade> gradeList = gradeService.getGradeList();
        
        view.setData(gradeList);
        
        return view;
    }
}